package com.kkb.mybatis.framework.sqlsession;

import com.kkb.mybatis.framework.config.Configuration;
import com.kkb.mybatis.framework.config.MappedStatement;
import com.kkb.mybatis.framework.executor.Executor;

import java.util.List;
import java.util.Map;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> List<T> selectList(String statementId, Map<String, Object> param) {
        //查询信息列表
        MappedStatement mappedStatement = configuration.getMappedStatementById(statementId);

        //Executor有多种类型
        Executor executor = configuration.newExecutor(null);

        //通过Executor处理JDBC
        return executor.query(configuration,mappedStatement,param);
    }

    @Override
    public <T> T selectOne(String statementId, Map<String, Object> param) {
        //查询单个信息
        List<Object> objects = this.selectList(statementId, param);
        if (objects != null && objects.size() == 1) {
            return (T) objects.get(0);
        }
        return null;
    }
}
